<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>5分钟概览typescript | haijd</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  <meta name="keywords" content="TypeScript" />
  
  
  
  
  <meta name="description" content="5分钟快速概览上手TypeScript">
<meta name="keywords" content="TypeScript">
<meta property="og:type" content="article">
<meta property="og:title" content="5分钟概览TypeScript">
<meta property="og:url" content="http://www.hais2.com/2018/04/15/5-minutes-to-fit-the-TypeScript/index.html">
<meta property="og:site_name" content="haijd">
<meta property="og:description" content="5分钟快速概览上手TypeScript">
<meta property="og:locale" content="default">
<meta property="og:image" content="https://lc-mhke0kuv.cn-n1.lcfile.com/4efd9a4018b57efdcf77.png">
<meta property="og:updated_time" content="2018-06-14T05:51:03.699Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="5分钟概览TypeScript">
<meta name="twitter:description" content="5分钟快速概览上手TypeScript">
<meta name="twitter:image" content="https://lc-mhke0kuv.cn-n1.lcfile.com/4efd9a4018b57efdcf77.png">
  
    <link rel="alternate" href="/atom.xml" title="haijd" type="application/atom+xml">
  
  <link rel="icon" href="/css/images/favicon.ico">
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link href="https://fonts.googleapis.com/css?family=Open+Sans|Montserrat:700" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,400italic" rel="stylesheet" type="text/css">
  <link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
  <style type="text/css">
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/9749f0/00000000000000000001008f/27/l?subset_id=2&fvd=n5) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/90cf9f/000000000000000000010091/27/l?subset_id=2&fvd=n7) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/8a5494/000000000000000000013365/27/l?subset_id=2&fvd=n4) format("woff2");font-weight:lighter;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/d337d8/000000000000000000010095/27/l?subset_id=2&fvd=i4) format("woff2");font-weight:400;font-style:italic;}</style>
    
  <link rel="stylesheet" id="athemes-headings-fonts-css" href="//fonts.googleapis.com/css?family=Yanone+Kaffeesatz%3A200%2C300%2C400%2C700&amp;ver=4.6.1" type="text/css" media="all">
  <link rel="stylesheet" href="/css/style.css">

  <script src="/js/jquery-3.1.1.min.js"></script>

  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="/css/bootstrap.css" >
  <link rel="stylesheet" href="/css/hiero.css" >
  <link rel="stylesheet" href="/css/glyphs.css" >
  
    <link rel="stylesheet" href="/css/vdonate.css" >
  

</head>

<script>
var themeMenus = {};

  themeMenus["/"] = "Home"; 

  themeMenus["/archives"] = "Archives"; 

  themeMenus["/categories"] = "Categories"; 

  themeMenus["/tags"] = "Tags"; 

  themeMenus["/about"] = "About"; 

</script>


  <body data-spy="scroll" data-target="#toc" data-offset="50">


  <header id="allheader" class="site-header" role="banner">
  <div class="clearfix container">
      <div class="site-branding">

          <h1 class="site-title">
            
              <a href="/" title="haijd" rel="home"> haijd </a>
            
          </h1>

          
            <div class="site-description">Stay Hungry,Stay Foolish</div>
          
            
          <nav id="main-navigation" class="main-navigation" role="navigation">
            <a class="nav-open">Menu</a>
            <a class="nav-close">Close</a>
            <div class="clearfix sf-menu">

              <ul id="main-nav" class="nmenu sf-js-enabled">
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/">Home</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/archives">Archives</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/categories">Categories</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/tags">Tags</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/about">About</a> </li>
                    
              </ul>
            </div>
          </nav>


      </div>
  </div>
</header>




  <div id="container">
    <div id="wrap">
            
      <div id="content" class="outer">
        
          <section id="main" style="float:none;"><article id="post-5-minutes-to-fit-the-TypeScript" style="width: 66%; float:left;" class="article article-type-post" itemscope itemprop="blogPost" >
  <div id="articleInner" class="clearfix post-1016 post type-post status-publish format-standard has-post-thumbnail hentry category-template-2 category-uncategorized tag-codex tag-edge-case tag-featured-image tag-image tag-template">
    
<div class="article-gallery">
  <div class="article-gallery-photos">
    
      <a class="article-gallery-img fancybox" href="https://lc-mhke0kuv.cn-n1.lcfile.com/4efd9a4018b57efdcf77.png" rel="gallery_cjie4namf008v3swaglads0ms">
        <img src="https://lc-mhke0kuv.cn-n1.lcfile.com/4efd9a4018b57efdcf77.png" itemprop="image">
      </a>
    
  </div>
</div>

    
      <header class="article-header">
        
  
    <h1 class="thumb" class="article-title" itemprop="name">
      5分钟概览TypeScript
    </h1>
  

      </header>
    
    <div class="article-meta">
      
	Posted on <a href="/2018/04/15/5-minutes-to-fit-the-TypeScript/" class="article-date">
	  <time datetime="2018-04-15T12:03:07.000Z" itemprop="datePublished">April 15, 2018</time>
	</a>

      
	<span id="busuanzi_container_page_pv">
	  本文总阅读量<span id="busuanzi_value_page_pv"></span>次
	</span>

    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <!-- TOC -->
<ul>
<li><a href="#">简介</a></li>
<li><a href="#">语法特性</a></li>
<li><a href="#">基础语法</a><ul>
<li><a href="#">声明语法：</a></li>
<li><a href="#">基础类型：</a></li>
<li><a href="#">属性和函数</a></li>
</ul>
</li>
</ul>
<!-- /TOC -->
<blockquote>
<p>5分钟快速上手，请猛击<a href="https://www.tslang.cn/docs/handbook/typescript-in-5-minutes.html" target="_blank" rel="noopener">这里</a>看官网文档。    </p>
</blockquote>
<h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p>　　TypeScript不是一种新的语言，可以认为是给JavaScript添加语言特性的语言扩展(例如在JavaScript还未出现import和装饰器的时候，TypeScript已经有了)，官方的说法是JavaScript的超集，最终还是会编译成JavaScript代码在浏览器执行。<br><a id="more"></a></p>
<h2 id="语法特性"><a href="#语法特性" class="headerlink" title="语法特性"></a>语法特性</h2><p>　　TypeScript是支持OOP开发的，对于有OOP经验的人来说是非常友好的，写过c#、Java、golang语言的开发者，很容易上手。<br>　　学习的时候，建议通读文档，中文文档非常全面，更新的也特别及时，建议多做参考，文档地址：<a href="https://www.tslang.cn/docs/home.html" target="_blank" rel="noopener">点此</a>！</p>
<h2 id="基础语法"><a href="#基础语法" class="headerlink" title="基础语法"></a>基础语法</h2><h3 id="声明语法："><a href="#声明语法：" class="headerlink" title="声明语法："></a>声明语法：</h3><ul>
<li><p>类、接口声明：</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> Name&#123;</span><br><span class="line">    <span class="comment">// 声明成员</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> IName&#123;</span><br><span class="line">    <span class="comment">// 声明成员</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="string">``</span><span class="string">`    </span></span><br><span class="line"><span class="string">- 变量声明:  </span></span><br><span class="line"><span class="string">`</span><span class="string">``</span>typescript</span><br><span class="line"><span class="keyword">let</span> num: <span class="built_in">number</span> = <span class="number">1</span>;  <span class="comment">// 显示赋值</span></span><br><span class="line"><span class="keyword">let</span> str: <span class="built_in">string</span>;      <span class="comment">// 默认初始值</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>函数声明：</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">func</span>(<span class="params">val: <span class="built_in">string</span></span>):<span class="title">void</span></span>&#123;</span><br><span class="line">    <span class="comment">// 该函数需要一个string类型的参数，无返回值。</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="基础类型："><a href="#基础类型：" class="headerlink" title="基础类型："></a>基础类型：</h3><p>TypeScript中的类型和JavaScript中某些是一致的，有：</p>
<ul>
<li>布尔类型：<code>boolean</code></li>
<li>数字类型：<code>number</code>   </li>
<li>字符串类型：<code>string</code>   </li>
<li>数组：<code>number[]</code>或者泛型方式<code>Array&lt;T&gt;</code></li>
<li><p>元组(Tuple)：<code>[string,number]</code></p>
<ul>
<li>元组也是一个数组，不同的是，元组是已知长度和类型的数组：<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> tup: [<span class="built_in">string</span>,<span class="built_in">number</span>];</span><br><span class="line">tup = [<span class="string">"str"</span>,<span class="number">123</span>];    <span class="comment">// 正确</span></span><br><span class="line">tup = [<span class="number">123</span>,<span class="string">"str"</span>];    <span class="comment">// 错误</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>枚举(enum):</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">enum</span> Color&#123;</span><br><span class="line">    Red = <span class="number">1</span>,</span><br><span class="line">    Blue,</span><br><span class="line">    Black</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>any：表示任意类型，不会在编译阶段对Any类型的变量进行类型检查，相当于c#中的<code>dynamic</code>。</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> notType: <span class="built_in">any</span> = <span class="number">123</span>;</span><br><span class="line">notType = <span class="string">"123"</span>;</span><br><span class="line">notType = <span class="literal">false</span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>void：这和any类型相反，表示没有任何类型，通常用在函数没有返回值的情况下，因为声明一个void类型没有什么用，void类型只能赋值undefined和null。</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> nothing: <span class="built_in">void</span> = <span class="literal">undefined</span>;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="属性和函数"><a href="#属性和函数" class="headerlink" title="属性和函数"></a>属性和函数</h3><ul>
<li>属性和函数都可以用public、protected和private修饰，默认的类、接口以及属性和函数都是public的，想要私有成员，需要显示的声明private；protected表示可以在当前类和它的子类中访问的成员。</li>
<li>属性可以声明只读属性：<code>private readonly _num: number = 123;</code></li>
<li>属性和函数都可以声明成静态属性/函数；包含静态成员的类，不需要new就可以调用其静态成员。静态成员中this关键字不指向声明它的类。</li>
<li><p>属性具有存取器<code>getter</code>和<code>setter</code>，可以是实现对属性的访问控制：</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> Person&#123;</span><br><span class="line">    <span class="keyword">private</span> _name: <span class="built_in">string</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">get</span> name():<span class="built_in">string</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>._name;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">set</span> name(val: <span class="built_in">string</span>)&#123;</span><br><span class="line">        <span class="keyword">this</span>._name = val;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>函数的参数中可以使用可选参数，表示调用该方法时，可以不传入该参数：</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> Person&#123;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">getName</span>(<span class="params">age: <span class="built_in">number</span>, gender?: <span class="built_in">string</span></span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">getName1</span>(<span class="params">age: <span class="built_in">number</span>,gender: <span class="built_in">string</span></span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> p :Person = <span class="keyword">new</span> Person();</span><br><span class="line">p.getName(<span class="number">23</span>);  <span class="comment">// 正确</span></span><br><span class="line">p.getName1(<span class="number">23</span>,<span class="string">"girl"</span>);  <span class="comment">// 正确</span></span><br><span class="line">p.getName1(<span class="number">23</span>)  <span class="comment">// 错误</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>函数参数可以设定默认值：</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> Person&#123;</span><br><span class="line">    function getName(age: number, gender = "girl")&#123;</span><br><span class="line">        <span class="keyword">return</span> age + <span class="string">"***"</span> + gender;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> p :Person = <span class="keyword">new</span> Person();</span><br><span class="line">p.getName(<span class="number">23</span>);  <span class="comment">// 23***girl</span></span><br><span class="line">p.getName(<span class="number">23</span>, <span class="literal">undefined</span>); <span class="comment">// 23***girl</span></span><br><span class="line">p.getName(<span class="number">23</span>, <span class="string">"boy"</span>);   <span class="comment">// 23***boy</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>TypeScript的特性还有很多，但是了解上面的简单特性基本上就能书写TypeScript代码了(可能对于没有接触过后端的开发者还是有点绕)，还是一句话，通读官方文档，那是非常好的入门和提升的资料。</p>

      
    </div>
    <footer class="entry-meta entry-footer">
      
	<span class="ico-folder"></span>
    <a class="article-category-link" href="/categories/Frontend/">Frontend</a>

      
  <span class="ico-tags"></span>
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/TypeScript/">TypeScript</a></li></ul>

      
        <div id="donation_div"></div>

<script src="/js/vdonate.js"></script>
<script>
var a = new Donate({
  title: '如果觉得我的文章对您有用，请随意打赏。您的支持将鼓励我继续创作!', // 可选参数，打赏标题
  btnText: 'Donate', // 可选参数，打赏按钮文字
  el: document.getElementById('donation_div'),
  wechatImage: '/css/images/wechatPay.png',
  alipayImage: '/css/images/alipay.jpg'
});
</script>
      

      
        
	<section id="comments" class="comment">
	  <div id="disqus_thread">
	  <noscript>Please enable JavaScript to view the <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
	  </div>
	</section>

	<script type="text/javascript">
	var disqus_shortname = 'haijdblog';
	(function(){
	  var dsq = document.createElement('script');
	  dsq.type = 'text/javascript';
	  dsq.async = true;
	  dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
	  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	}());
	(function(){
	  var dsq = document.createElement('script');
	  dsq.type = 'text/javascript';
	  dsq.async = true;
	  dsq.src = '//' + disqus_shortname + '.disqus.com/count.js';
	  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	}());
	</script>


      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2018/05/30/Spring-AOP-Minimalist-Getting-Started/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Spring-AOP极简入门
        
      </div>
    </a>
  
  
    <a href="/2017/12/10/Java-compareTo-String-Function/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Java compareTo字符串比较函数</div>
    </a>
  
</nav>

  
</article>

<!-- Table of Contents -->

  <aside id="sidebar">
    <div id="toc" class="toc-article">
    <strong class="toc-title">Contents</strong>
    
      <ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#简介"><span class="nav-number">1.</span> <span class="nav-text">简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#语法特性"><span class="nav-number">2.</span> <span class="nav-text">语法特性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#基础语法"><span class="nav-number">3.</span> <span class="nav-text">基础语法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#声明语法："><span class="nav-number">3.1.</span> <span class="nav-text">声明语法：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#基础类型："><span class="nav-number">3.2.</span> <span class="nav-text">基础类型：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#属性和函数"><span class="nav-number">3.3.</span> <span class="nav-text">属性和函数</span></a></li></ol></li></ol>
    
    </div>
  </aside>
</section>
        
      </div>
      <footer id="footer" class="site-footer">
  

  <div class="clearfix container">
      <div class="site-info">
	      &copy; 2018 haijd All Rights Reserved.
          
            <span id="busuanzi_container_site_uv">
              本站访客数<span id="busuanzi_value_site_uv"></span>人次  
              本站总访问量<span id="busuanzi_value_site_pv"></span>次
            </span>
          
      </div>
      <div class="site-credit">
        Theme by <a href="https://github.com/iTimeTraveler/hexo-theme-hiero" target="_blank">hiero</a>
      </div>
  </div>
</footer>


<!-- min height -->

<script>
    var contentdiv = document.getElementById("content");

    contentdiv.style.minHeight = document.body.offsetHeight - document.getElementById("allheader").offsetHeight - document.getElementById("footer").offsetHeight + "px";
</script>
    </div>
    <!-- <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
    <a href="/categories" class="mobile-nav-link">Categories</a>
  
    <a href="/tags" class="mobile-nav-link">Tags</a>
  
    <a href="/about" class="mobile-nav-link">About</a>
  
</nav> -->
    

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/scripts.js"></script>
<script src="/js/bootstrap.js"></script>
<script src="/js/main.js"></script>








	<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
	</script>






  </div>

  <a id="rocket" href="#top" class=""></a>
  <script type="text/javascript" src="/js/totop.js" async=""></script>
</body>
</html>
